package com.sc.userspringboot.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sc.userspringboot.dao.ICommentsDao;
import com.sc.userspringboot.dao.IPostsDao;
import com.sc.userspringboot.pojo.Comments;
import com.sc.userspringboot.pojo.Follows;
import com.sc.userspringboot.pojo.Posts;
import com.sc.userspringboot.pojo.Users;
import com.sc.userspringboot.service.ICommentsService;
import com.sc.userspringboot.service.IFollowsService;
import com.sc.userspringboot.service.IPostsService;
import com.sc.userspringboot.utils.JwtUtils;
import com.sc.userspringboot.utils.ResponseMsg;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/hotStop")
public class HotStopController {
    @Autowired
    IPostsService iPostsService;

    @Autowired
    ICommentsService iCommentsService;

    @Autowired
    IFollowsService iFollowsService;

    @RequestMapping("getHotStop")
    ResponseMsg getHotStop()
    {
        //暂时获取的随机帖子,暂时用作轮播图
        List<Posts> randomPosts = iPostsService.getByPostId_limit(10);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,randomPosts);
    }

    @RequestMapping("getByPostId/{postId}")
    ResponseMsg getByPostId(@PathVariable Integer postId){
        //获取具体的帖子
        Posts posts = iPostsService.getByPostId_id(postId);
        posts.setCommentCount(iCommentsService.getNumber(postId));
        iPostsService.updateById(posts);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,posts);
    }

    @RequestMapping("getSort")
    ResponseMsg getSort(){
        //获取帖子热度排序
        List<Posts> posts = iPostsService.getSort();
        posts.forEach(posts1 -> posts1.setCommentCount(iCommentsService.getNumber(posts1.getPostId())));
        posts.forEach(posts1 -> iPostsService.updateById(posts1));
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,posts);
    }

    @RequestMapping("getGamesId/{gameId}/{page}")
    ResponseMsg getGamesId(@PathVariable Integer gameId,@PathVariable Integer page){
        //获取游戏帖子
        List<Posts> posts = iPostsService.getGamesId(gameId,page);
        posts.forEach(posts1 -> posts1.setCommentCount(iCommentsService.getNumber(posts1.getPostId())));
        posts.forEach(posts1 -> iPostsService.updateById(posts1));
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,posts);
    }

    @RequestMapping("getPublishTime/{page}")
    ResponseMsg getPublishTime(@PathVariable Integer page){
        //根据帖子发布时间逆序排版
        List<Posts> posts = iPostsService.getPublishTime(page);
        posts.forEach(posts1 -> posts1.setCommentCount(iCommentsService.getNumber(posts1.getPostId())));
        posts.forEach(posts1 -> iPostsService.updateById(posts1));
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,posts);
    }

    @RequestMapping("text")
    ResponseMsg text(){
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,iPostsService.getByPostId_id(1));
    }

    @RequestMapping("commentsadd")//添加评论
    ResponseMsg commentsadd(@RequestBody Comments comments, HttpServletRequest request){
        String jwtIoken = request.getHeader("token");
        try {
            Claims claims = JwtUtils.parseJWT(jwtIoken);
            Users u = JSON.parseObject(claims.getSubject(), Users.class);
            comments.setUserId(u.getUserId());
            comments.setCreateTime(new Date());
        } catch (Exception e) {//1:格式错误,2:过期
            System.out.println("jwt错误:"+e.getMessage());
            return null;
        }
        Posts posts = iPostsService.getByPostId_id(comments.getPostId());
        posts.setCommentCount(iCommentsService.getNumber(comments.getPostId()));
        iPostsService.updateById(posts);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,iCommentsService.save(comments));
    }

    @RequestMapping("isFollowed")
    ResponseMsg isFollowed(@RequestBody Follows follows, HttpServletRequest request){
        String jwtIoken = request.getHeader("token");
        try {
            Claims claims = JwtUtils.parseJWT(jwtIoken);
            Users u = JSON.parseObject(claims.getSubject(), Users.class);
            follows.setFollowerId(u.getUserId());
            follows.setFollowTime(new Date());
            Follows isFollowed = iFollowsService.isFollowed(follows.getFollowerId(),follows.getFolloweeId());
            if (isFollowed == null){
                iFollowsService.save(follows);
                return ResponseMsg.getInstance(ResponseMsg.SUCCESS,1);
            } else {
                return ResponseMsg.getInstance(ResponseMsg.SUCCESS,isFollowed.getIsCancel());
            }
        } catch (Exception e) {//1:格式错误,2:过期
            System.out.println("jwt错误:"+e.getMessage());
            return null;
        }
    }

    @RequestMapping("isFollowedChange")
    ResponseMsg isFollowedChange(@RequestBody Follows follows, HttpServletRequest request){
        String jwtIoken = request.getHeader("token");
        try {
            Claims claims = JwtUtils.parseJWT(jwtIoken);
            Users u = JSON.parseObject(claims.getSubject(), Users.class);
            follows.setFollowerId(u.getUserId());
            Follows isFollowed = iFollowsService.isFollowed(follows.getFollowerId(),follows.getFolloweeId());
            if (isFollowed.getIsCancel() == 0){
                isFollowed.setIsCancel((byte) 1);
            } else {
                isFollowed.setIsCancel((byte) 0);
            }
            isFollowed.setFollowTime(new Date());
            iFollowsService.updateById(isFollowed);
        } catch (Exception e) {//1:格式错误,2:过期
            System.out.println("jwt错误:"+e.getMessage());
            return null;
        }
        return ResponseMsg.success();
    }
}
